package ase.assignment.sts.utils;

import org.jdom.*;
import org.jdom.input.*;
import java.io.*;
import java.util.List;
import java.util.HashMap;

public class ActionConfigReader{
	
	// config xml file
	String configFile = null;

	// hashmap used to store action
	HashMap actionMap;

	// ctor
	public ActionConfigReader(String _conffile ){
		configFile = _conffile;
		actionMap = new HashMap();
		
	}


	//
	// Public method begins

	public boolean hasMoreActionConfigs(){
		return true;
	}

	public HashMap parse(){

		SAXBuilder builder = new SAXBuilder();
		try{

			Document config = builder.build(new FileInputStream(configFile));	
			Element root = config.getRootElement();		
			List actionList = root.getChildren("action");			
			for( int i = 0; i < actionList.size(); i++ ){
				Element eAction = (Element)actionList.get(i);
				makeAction(eAction);
			}

;

		}catch(IOException e)
		{
			e.printStackTrace();
		}
		catch( JDOMException e)
		{
			e.printStackTrace();
		}
		catch( IllegalStateException e )
		{
			e.printStackTrace();
		}

		return actionMap;
	}

	//
	// Private method begins
	private void makeAction(Element action){
		String actionName = action.getAttribute("name").getValue();
		ActionConfig ac = new ActionConfig();
		//
		// get class name
		ac.setClassName(action.getChild("class").getAttribute("name").getValue());
		System.out.println(" ActionConfig : class <name> " + ac.getClassName());
		// get forward url
		ac.setForwardURL(action.getChild("forward").getAttribute("url").getValue());
		System.out.println(" ActionConfig : forward <url> " + ac.getForwardURL());
		
		actionMap.put(actionName, ac );
	
	}
	

}
